package com.sgu6.sgu6_machine.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.sgu6.sgu6_machine.R;
import com.sgu6.sgu6_machine.config.MotorConfig;
import com.sgu6.sgu6_machine.listener.OnMotorButtonClickListener;

import java.util.List;

public class MotorAdapter extends BaseAdapter implements View.OnClickListener, TextWatcher {

    private List<MotorConfig> motors;
    private LayoutInflater inflater;
    private OnMotorButtonClickListener listener;

    public MotorAdapter(Context context, List<MotorConfig> motors, OnMotorButtonClickListener listener) {
        this.motors = motors;
        this.inflater = LayoutInflater.from(context);
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return motors.size();
    }

    @Override
    public MotorConfig getItem(int position) {
        return motors.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {

        MotorHolder holder;

        if (view == null) {
            view = inflater.inflate(R.layout.item_motor, parent, false);
            holder = new MotorHolder();
            holder.name = view.findViewById(R.id.label);
            holder.count = view.findViewById(R.id.tv_count);
            holder.gap = view.findViewById(R.id.et_gap);
            holder.times = view.findViewById(R.id.et_times);
            holder.launcherTime = view.findViewById(R.id.et_launcher_time);
            holder.btnReset = view.findViewById(R.id.btn_reset);
            holder.btnStart = view.findViewById(R.id.btn_start);
            holder.btnStop = view.findViewById(R.id.btn_stop);
            view.setTag(holder);
        } else {
            holder = (MotorHolder) view.getTag();
        }

        holder.name.setText(motors.get(position).getName());
        holder.times.setText(String.valueOf(motors.get(position).getTimes()));
        holder.count.setText(String.valueOf(motors.get(position).getCount()));
        holder.gap.setText(String.valueOf(motors.get(position).getGap()));
        holder.launcherTime.setText(String.valueOf(motors.get(position).getLauncherTime()));
        holder.btnReset.setOnClickListener(this);
        holder.btnReset.setTag(position);
        holder.btnStart.setOnClickListener(this);
        holder.btnStart.setTag(position);
        holder.btnStop.setOnClickListener(this);
        holder.btnStop.setTag(position);

        holder.times.addTextChangedListener(this);
        holder.launcherTime.addTextChangedListener(this);
        holder.gap.addTextChangedListener(this);

        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_start:
                listener.onStartClick(v);
                break;
            case R.id.btn_stop:
                listener.onStopClick(v);
                break;
            case R.id.btn_reset:
                listener.onResetClick(v);
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    static class MotorHolder {
        TextView name, count;
        EditText times, gap, launcherTime;
        Button btnStart, btnStop, btnReset;
    }

}
